package com.github.baichuan.application.scaffold.dto.audit;

import com.github.baichuan.immutable.Tuple2;
import com.github.baichuan.web.dto.DateRange;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import lombok.Getter;

import java.util.LinkedList;

@Schema(title = "用户操作日志查询条件")
@Getter
public class AuditLogQueryCriteria {
    @Schema(title = "应用")
    @NotBlank(message = "应用不能为空")
    private String application;

    @Schema(title = "操作人")
    private String operator;

    @Schema(title = "操作时间")
    private DateRange operationTime;

    @Schema(title = "名称/代码")
    private String keyword;

    public Tuple2<String, Object[]> where(){
        LinkedList list = new LinkedList();

        StringBuilder sql = new StringBuilder();
        sql.append("application=?");
        list.add(application);

        if (operator != null){
            sql.append(" and operator=?");
            list.add(operator);
        }

        if (operationTime != null){
            sql.append(" and operation_time between ? and ?");
            list.add(operationTime.getBegin());
            list.add(operationTime.getEnd());
        }

        if(keyword != null){
            keyword = keyword.trim();
            sql.append(" and (name like ? or code like ?)");
            list.add("%" + keyword + "%");
            list.add("%" + keyword + "%");
        }
        return Tuple2.of(sql.toString(), list.toArray());
    }
}
